package cn.jbit.houserent.action;

import cn.jbit.houserent.bean.User;
import cn.jbit.houserent.biz.UserBiz;
import cn.jbit.houserent.biz.impl.UserBizImpl;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {

	private static final long serialVersionUID = 537099716868797289L;
	private String name;
	private String password;
	private String repassword;
	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	private String telephone;
	private String username;
	private String message;


	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	private String action;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

	public String register() throws Exception {
		if(name == null || name.equals("")){
			this.setMessage("用户名和密码不能为空");
			return INPUT;
		}else if(password == null || password.equals("")){
			this.setMessage("用户名和密码不能为空");
			return INPUT;
		}else if(repassword == null || repassword.equals("")||repassword!=password){
			this.setMessage("密码与确认密码不一致");
			return INPUT;
		}else{
			User user = new User();
			user.setName(name);
			user.setPassword(password);
			user.setTelephone(telephone);
			user.setUsername(username);
			UserBiz ub = new UserBizImpl();
			if(!ub.validate(user.getName().trim())) {
				this.setMessage("用户名已存在");
				return INPUT;
			}
			if(!ub.register(user)) return INPUT;
			return SUCCESS;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String login() throws Exception {
		ActionContext ac = ActionContext.getContext();
		if(name == null || name.equals("")){
			this.setMessage("登录失败，请检查用户名和密码是否正确");
			return INPUT;
		}else if(password == null || password.equals("")){
			this.setMessage("登录失败，请检查用户名和密码是否正确");
			return INPUT;
		}
		UserBiz ub = new UserBizImpl();
		User user = ub.login(name, password);
		if(null==user){
			this.setMessage("登录失败，请检查用户名和密码是否正确");
			return INPUT;
		}else{
			//保存用户信息到Session中
			ac.getSession().put("user", user);
			return SUCCESS;
		}
				
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getAction() {
		return action;
	}

}